package com.store.pet.petstore.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.store.pet.petstore.R;
import com.store.pet.petstore.bean.MessageBean;
import com.store.pet.petstore.util.Constant;
import com.store.pet.petstore.util.GsonUtils;
import com.store.pet.petstore.util.OKHttpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.store.pet.petstore.fragment.PersonFragment.CHANGE_SUCCESS;

public class ChangePasswordActivity extends BaseActivity {

    @BindView(R.id.my_status)
    View myStatus;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.old_password_et)
    EditText oldPasswordEt;
    @BindView(R.id.new_password_et1)
    EditText newPasswordEt1;
    @BindView(R.id.new_password_et2)
    EditText newPasswordEt2;
    @BindView(R.id.determine)
    Button determine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_change_password;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        //沉浸式标题栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        takeLinearStatusBar(myStatus);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.drawable.fanhui);  //设置左上角图标
        toolbar.setNavigationOnClickListener(view -> finish());  //退出
    }

    @Override
    public void loadData() {

    }

    @OnClick(R.id.determine)
    public void onViewClicked() {
        String oldPsd = oldPasswordEt.getText().toString();  //旧密码
        String newPsd1 = newPasswordEt1.getText().toString();  //新密码
        String newPsd2 = newPasswordEt2.getText().toString();  //再次输入新密码
        if(!oldPsd.equals("") && !newPsd1.equals("") && !newPsd2.equals("")){
            if(newPsd1.equals(newPsd2)){
                Map<String,Object> map = new HashMap<>();
                map.put("userId",getMyId());
                map.put("userpsd",oldPsd);
                map.put("newpsd",newPsd1);
                OKHttpUtils.requestData(Constant.CHANGE_PASSWORD, map, new OKHttpUtils.ServerCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        MessageBean msg = GsonUtils.parseMessage(data);
                        if(msg.getCode() == 106){
                            ToastUtils.showShort("修改成功，请重新登录");
                            setResult(CHANGE_SUCCESS,new Intent());
                            finish();
                        }else {
                            ToastUtils.showShort(msg.getMsg());
                        }

                    }

                    @Override
                    public void onFailed(String message) {

                    }
                });
            }else {
                ToastUtils.showShort("两次密码不一致");
            }
        }else{
            ToastUtils.showShort("请输入完整");
        }
    }
}
